package net.gz01.wallet.web.service;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

import com.alibaba.fastjson.JSON;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;

import net.gz01.util.ZODate;
import net.gz01.wallet.message.AccountBalanceChangeResp;
import net.gz01.wallet.message.AccountTransferResp;
import net.gz01.wallet.web.model.Account;

public class AccountService {

	public void addAccount(String merchCode, String name) {
		Account ac = new Account();
		Account  acff = Account.dao.select(merchCode);
		if(acff!=null && acff.getAccountNo()!=null){
			return;
		}
		ac.setAccountNo(merchCode);
		ac.setAccountName(name);
		ac.setBalance(0L);
		ac.setEnable(true);
		ac.setCreateDateTime(ZODate.getDateTime());
		ac.save();
	}

	public AccountBalanceChangeResp addMoney(String accountNo, Long amount) {
		AccountBalanceChangeResp changeBalanceRsp = changeBalance(accountNo, Math.abs(amount));
		System.out.println("虚账返回 <--" + JSON.toJSONString(changeBalanceRsp));
		return changeBalanceRsp;
	}

	public AccountBalanceChangeResp subMoney(String accountNo, Long amount) {
		AccountBalanceChangeResp changeBalanceRsp = changeBalance(accountNo, -Math.abs(amount));
		System.out.println("虚账返回 <--" + JSON.toJSONString(changeBalanceRsp));
		return changeBalanceRsp;
	}

	/**
	 * 操作虚账金额
	 *
	 * @param accountNo
	 * @param amount
	 * @return
	 */
	public AccountBalanceChangeResp changeBalance(String accountNo, Long amount) {
		Object result = Db.execute(new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				CallableStatement proc = conn.prepareCall("{ call balance_change_proc(?,?,?,?) }");
				proc.setString(1, accountNo);
				proc.setLong(2, amount.longValue());
				proc.registerOutParameter(3, java.sql.Types.VARCHAR);
				proc.registerOutParameter(4, java.sql.Types.BIGINT);
				proc.execute();
				AccountBalanceChangeResp rsp = new AccountBalanceChangeResp();
				rsp.setRespMsg(proc.getString(3));
				rsp.setRespCode(proc.getString(3));
				rsp.setBalance(proc.getString(4));
				return rsp;
			}
		});
		return (AccountBalanceChangeResp) result;
	}

	/**
	 * 虚账转账
	 *
	 * @param fromAccountNo
	 *            转出虚账账号
	 * @param toAccountNo
	 *            转入虚账账号
	 * @param amount
	 *            金额分
	 * @return
	 */
	public AccountTransferResp transfer(String fromAccountNo, String toAccountNo, Long amount) {
		Object result = Db.execute(new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				CallableStatement proc = conn.prepareCall("{ call transfer_proc(?,?,?,?,?,?) }");
				proc.setString(1, fromAccountNo);
				proc.setString(2, toAccountNo);
				proc.setLong(3, amount);
				proc.registerOutParameter(4, java.sql.Types.BIGINT);
				proc.registerOutParameter(5, java.sql.Types.BIGINT);
				proc.registerOutParameter(6, java.sql.Types.VARCHAR);
				proc.execute();
				AccountTransferResp rsp = new AccountTransferResp();
				rsp.setFromAccountBalance(proc.getString(4));
				rsp.setToAccountBalance(proc.getString(5));
				rsp.setRespCode(proc.getString(6));
				rsp.setRespMsg(proc.getString(6));
				return rsp;
			}
		});
		return (AccountTransferResp) result;
	}

}
